package com.jz.community.basecomm.base.baseSupport;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.mvp.MVPContract;

/**
 * Created by Tony.Fan on 2018/3/15 14:51
 */
public abstract class BaseMvpFragment<V extends MVPContract.View, P extends BaseMvpPresenter<V>> extends SupportFragment {

    protected P mPresenter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initPresenter();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    private void initPresenter() {
        if (mPresenter == null) {
            mPresenter = createPresenter();
        }
        mPresenter.attachView((V) this);
    }

    protected abstract P createPresenter();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }
}
